<?php
namespace app\index\controller;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 库存控制器
 */
class Stocks extends Base{
   /**
	* 查看库存列表
	*/
	public function index(){
        session("shopId",session("HTR_STAFF.shopId"));
        $this->assign("authority",(session("HTR_STAFF.staffRoleId") == 1 || (session("HTR_STAFF.staffRoleId") == 2 && session("HTR_STAFF.shopId"))) ? 1 : 0);
        $this->assign("areaList",model('areas')->listQuery(0));

        $shopList = model('shops')->field('shopId,shopSn,shopName')->where(['dataFlag'=>1])->select();
        $this->assign("shopList",$shopList);
        $this->assign("shopId",session("HTR_STAFF.shopId"));
		return $this->fetch();
	}
    /**
     * 获取分页
     */
    public function pageQuery(){
        return model('Stocks')->pageQuery();
    }
    /**
     * 跳去编辑页面
     */
    public function toEdit(){
        $id = input("id/d",0);
        $object = model('Stocks')->get($id);
        if($object){
            $goods = model('goods')->field('goodsId,goodsSn,goodsName')->where(['goodsId'=>$object['goodsId']])->find();
            $shop = model('shops')->field('shopId,shopSn,shopName')->where(['shopId'=>$object['shopId']])->find();
            $this->assign("shop",$shop);
            $this->assign("goods",$goods);
        }else{            
            $goodsList = model('goods')->field('goodsId,goodsSn,goodsName')->where(['dataFlag'=>1])->select();
            $this->assign("goodsList",$goodsList);
            if(session("HTR_STAFF.shopId")){
                $shop = model('shops')->field('shopId,shopSn,shopName')->where(['shopId'=>session("HTR_STAFF.shopId")])->find();
                $this->assign("shop",$shop);
            }else{
                $shopList = model('shops')->field('shopId,shopSn,shopName')->where(['dataFlag'=>1])->select();
                $this->assign("shopList",$shopList);
            }
        }    
        $this->assign("shopId",session("HTR_STAFF.shopId"));
        $this->assign("object",$object);
        return $this->fetch("edit");
    }
    /**
     * 新增
     */
    public function add(){
        return model('Stocks')->add();
    }
    /**
     * 编辑
     */
    public function edit(){
        return model('Stocks')->edit();
    }
    /**
     * 编辑
     */
    public function upStock(){
        return model('Stocks')->upStock();
    }
    /**
     * 删除库存
     */
    public function del(){
        return model('Stocks')->del();
    }
    /**
     * 扫码出库
     */
    public function scan(){
        if(!session("HTR_STAFF.shopId")){
            $shopList = model('shops')->field('shopId,shopSn,shopName')->where(['dataFlag'=>1])->select();
            $this->assign("shopList",$shopList);
        }
        $this->assign("shopId",session("HTR_STAFF.shopId"));
        return $this->fetch();
    }
    public function reduce(){        
        $goodsSn = input('post.goodsSn');
        if($goodsSn == '')return HTRReturn('产品编号不能为空');
        $goodsId = model('goods')->where(['dataFlag'=>1,'goodsSn'=>$goodsSn])->value('goodsId');
        if(!$goodsId)return HTRReturn('产品编号不存在');
        $shopId = session("shopId");
        if(!$shopId)return HTRReturn('请选择店铺');
        return model('stockLog')->add($goodsId,$shopId,0,1,'扫码出库');
    }
    public function setShop(){        
        $shopId = input('post.shopId');
        if(!$shopId)return HTRReturn('申请选择店铺');
        session("shopId",$shopId);
        return HTRReturn("选择成功", 1);
    }
}
